//
//  AFFRichTextHtml.m
//  AnyfishApp
//
//  Created by Bob Lee on 14-6-3.
//  Copyright (c) 2014年 Anyfish. All rights reserved.
//
/*((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(((http[s]{0,1}|ftp)://)?((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?))(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?|*/

/*原: (^(((ht|f)tp(s?))\://)?(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\;\?\'\\\+&amp;%\$#\=~_\-]+))*$) */

/*改1: (^(((ht|f)tp(s?))\\://)?(www.|[a-zA-Z].)[a-zA-Z0-9\\-\\.]+\\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\\:[0-9]+)*(/($|[a-zA-Z0-9\\.\\\\,\\;\\?\\'\\\\+&amp;%\\$#\\=~_\\-]+))*$) */

/*改2: (^(((ht|f)tp(s?))://)?(www.|[a-zA-Z].)[a-zA-Z0-9-.]+.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(:[0-9]+)*(/($|[a-zA-Z0-9.,;?'\\+&amp;%$#=~-]+))*$) */

#import "AFFRichTextHtml.h"

#define kRegularExp_Http @"(^(((ht|f)tp(s?))\\://)?(www.|[a-zA-Z].)[a-zA-Z0-9\\-\\.]+\\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\\:[0-9]+)*(/($|[a-zA-Z0-9\\.\\\\,\\;\\?\\'\\\\+&amp;%\\$#\\=~_\\-]+))*$)|((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|((((http[s]{0,1}|ftp)://)?((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?))(\\:[0-9]+)(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)"


#define kRegularExp_Html @"<(!|/)?\\w+( ((.|\\n)*?"")?)? *>"

@implementation AFFRichTextHtml

- (id)init
{
    self = [super init];
    if (self) {
        self.textType = ERichTextType_Html;
        self.isResponseTouch = YES;
        self.textColor = kSetting.color_2984e9;
    }
    return self;
}

//-- 替换基础文本
- (void)replaceTextWithAttributedString:(NSMutableAttributedString*) attributedString
{
    if(self.textColor)
        [attributedString addAttribute:(NSString *)kCTForegroundColorAttributeName value:(id)self.textColor.CGColor range:self.range];
    
    if(self.originalFont)
        [attributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)self.originalFont range:self.range];
    
    NSDictionary *underlineAttribute = @{NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle)};
    [attributedString addAttributes:underlineAttribute range:self.range];
    [super replaceTextWithAttributedString:attributedString];
}

//-- 绘制内容
- (BOOL)drawRunWithRect:(CGRect)rect
{
    return NO;
}

//-- 解析文本内容
+ (NSString *)analyzeText:(NSString *)string runsArray:(NSMutableArray **)runArray
{
    NSError *error;
    NSString *regulaStr = kRegularExp_Http;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    NSArray *arrayOfAllMatches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
    
    for (NSTextCheckingResult *match in arrayOfAllMatches)
    {
        NSString* substringForMatch = [string substringWithRange:match.range];
        AFFRichTextHtml *urlRun = [[AFFRichTextHtml alloc] init];
        urlRun.range = match.range;
        urlRun.originalText = substringForMatch;
        [*runArray addObject:urlRun];
    }
    
    return [string copy];
}

/**
 判断用户发送的文本是否仅位超链接
 */
+ (BOOL)isMediaUrl:(NSString *)string result:(NSString **)result
{
    if([NSString isNilOrEmpty:string])
        return NO;
    
    NSError *error;
    NSString *regulaStr = kRegularExp_Http;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regulaStr
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    NSArray *arrayOfAllMatches = [regex matchesInString:string options:0 range:NSMakeRange(0, [string length])];
    if(!arrayOfAllMatches||arrayOfAllMatches.count<=0)
        return NO;
    
    NSTextCheckingResult *match = arrayOfAllMatches[0];
    BOOL isUrl = match.range.length == [string length];
    if(isUrl){
        if(result)
            *result = [AFFRichTextHtml appendHttp:string];
    }
    return isUrl;
}

+ (NSString *)appendHttp:(NSString *)source{
    NSError *error;
    NSString *result = source;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[a-zA-z]+://[^\\s]*"
                                                      options:NSRegularExpressionCaseInsensitive
                                                        error:&error];
    NSArray *arrayOfAllMatches = [regex matchesInString:source options:0 range:NSMakeRange(0, [source length])];
    if(arrayOfAllMatches && arrayOfAllMatches.count<=0){
        result = [NSString stringWithFormat:@"http://%@",source];
    }
    
    return result;
}

@end
